<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>邀请好友</title>
    <link rel="stylesheet" href="static/css/aui.2.0.css">
    <link rel="stylesheet" href="static/css/alert.css">
    <link rel="stylesheet" href="static/css/share.css">
    <style>
        .share-pass{
            display: inline-block !important;
            margin-top: 1vh !important;
        }
    </style>
</head>
<body>
    <section class="title-box">
        <div class="title-max">多卖宝</div>
        <div class="title-little">消费多返利多</div>
    </section>
    <section class="share-box">
        <img src="" class="user-pic" alt="">
        <div class="share-content">
            <div class="share-people">
                <div class="share-people-one">
                    <span class="user-name"></span> 送您一个现金红包
                </div>
                <div>邀请您成为TA的多卖宝好友</div>
            </div>
            <div class="share-login-code">注册码: <span class="user-res-code"></span></div>
            <div class="share-line">
                <div class="line"></div>
                <div>全场通用</div>
                <div class="line"></div>
            </div>
            <div class="share-input">
                <input type="tel" class="share-number" placeholder="请填写手机号">

                <div class="share-code-box">
                    <input type="number" class="share-code" placeholder="请输入验证码">
                    <div class="input-line"></div>
                    <input id="get-code" readonly class="disabled" value="获取验证码">
                </div>
                <input type="password" class="share-number share-pass" placeholder="请输入密码">
            </div>
            <div class="pull-hongbao">下一步</div>
        </div>
    </section>

    <script src="lib/jquery-3.0.0.min.js"></script>
    <script src="static/js/config.js"></script>
    <script src="static/js/alert.js"></script>
    <script src="static/js/function.js"></script>
    <script>
        var wait=60;
        var userInfoId = locationSearcher('invitationidId');
        var loading = dialog.loading();
        $.ajax({
            url:C.interface.getShareUserInfo,
            type:'post',
            dataType:'json',
            data:{
                phone:userInfoId
            },
            complete:function () {
                loading.close();
            },
            success:function (response) {
                if (response.errorCode == '200'){
                    var data = response.data;
                    var userImgUrl = data.HEAD_IMG;
                    var userName = data.NICK_NAME;
                    var userRegisteCode = data.PHONE;
                    $('.user-pic').attr('src',userImgUrl);
                    $('.user-name').html(userName);
                    $('.user-res-code').html(userRegisteCode);

                    $('.disabled').bind('click',function () {
                        var that = this;
                        var phone = $('.share-number').val();

                        var validate = RegExp(/^1[3|4|5|8|7][0-9]\d{4,8}$/).test(phone);
                        //验证用户是否输入了手机号
                        if(phone==''){
                            dialog.tusiError('请输入您的手机号',1000);
                            return false;
                        }
                        if (!validate){
                            dialog.tusiError('请输入正确的手机号',1000);
                            return false;
                        }
                        getCode(that,phone);

                    });
                    $('.pull-hongbao').unbind().bind('click',function () {
                        registerSuccess(userRegisteCode)
                    });


                }else {
                    dialog.tusiError(response.errorMsg);
                }
            }
        });




        
        function registerSuccess(registerCode) {
            var registerPhone = $('.share-number').val();
            var pass = $('.share-pass').val();
            var code = $('.share-code').val();
            var validate = RegExp(/^1[3|4|5|8|7][0-9]\d{4,8}$/).test(registerPhone);
            //验证用户是否输入了手机号
            if(registerPhone==''){
                dialog.tusiError('请输入您的手机号',1000);
                return false;
            }
            if (!validate){
                dialog.tusiError('请输入正确的手机号',1000);
                return false;
            }
            if (code == ''){
                dialog.tusiError('请输入验证码',1000);
                return false;
            }
            if (pass.length < 6 || pass == ''){
                dialog.tusiError('密码需大于6位');
                return false;
            }
            var loading = dialog.loading();
            $.ajax({
                url:C.interface.shareRegister,
                type:'post',
                dataType:'json',
                data:{
                    phone:registerPhone,
                    pwd:pass,
                    code:code,
                    topUserId:userInfoId
                },
                complete:function () {
                    loading.close();
                },
                success:function (response) {
                    if (response.errorCode == '200'){
                        window.location.href = 'share_success.html?invitationidId=' + userInfoId ;
                    }else {
                        dialog.tusiError(response.errorMsg);
                    }
                }

            })
        }


        /*这里后台请求获取验证码*/
        function getCode(that,tel) {
            var loading = dialog.loading();
            $.ajax({
                url:C.interface.getCode,
                type:'post',
                dataType:'json',
                data:{
                    phone:tel
                },
                complete:function () {
                    loading.close();
                },
                success:function (response) {
                    if (response.errorCode == '200'){
                        //改变为倒计时
                        time(that);
                    }else {
                        dialog.tusiError(response.errorMsg);
                    }
                }
            })
        }

        function time(o) {
            if (wait == 0) {
                o.removeAttribute("disabled");
                o.value="获取验证码";
                wait = 60;
            } else {

                o.setAttribute("disabled", true);

                o.value=wait +'s';
                wait--;
                setTimeout(function() {
                            time(o)
                }, 1000);

            }
        }
    </script>

</body>
</html>